/*
 * @lc app=leetcode.cn id=530 lang=cpp
 *
 * [530] 二叉搜索树的最小绝对差
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<int> res;
    int getMinimumDifference(TreeNode *root)
    {
        //题目限制节点数大于等于2
        traval(root);
        int minres = __INT_MAX__;
        for (int i = 0; i < res.size() - 1; i++)
        {
            minres = min(minres, res[i + 1] - res[i]);
        }
        return minres;
    }
    void traval(TreeNode *root)
    {
        if (root == NULL)
        {
            return;
        }
        traval(root->left);
        res.push_back(root->val);
        traval(root->right);
    }
};
// @lc code=end
